package com.sz.business.dao.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import java.util.HashMap;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;


import com.sz.business.dao.IBusinessDAO;

import com.sz.common.SZUtility;

import com.sz.common.ICommonDAO;



public class BusinessDAO extends SqlMapClientDaoSupport implements IBusinessDAO{
	
	protected final Log logger = LogFactory.getLog(getClass());
	
	private ICommonDAO commonDAO = null;
	
	public ICommonDAO getCommonDAO() {
		return commonDAO;
	}

	public void setCommonDAO(ICommonDAO commonDAO) {
		this.commonDAO = commonDAO;
	}

	@SuppressWarnings("unchecked")
	public List queryBusiness(Map map) throws Exception {		
		List list = getSqlMapClient().queryForList("business_sqlmap.query_Business", map);
		return addition(list);
	}
	
	@SuppressWarnings("unchecked")
	public List queryBusinessDetail(Map map) throws Exception{
		List list = getSqlMapClient().queryForList("business_sqlmap.query_Business_Detail", map);
		return addition(list);	
	}
	
	@SuppressWarnings("unchecked")
	public List ModifyBusinessDetail(Map map) throws Exception{
		List list = getSqlMapClient().queryForList("business_sqlmap.Modify_Business_Detail", map);
		return addition(list);	
	}
	
	@SuppressWarnings("unchecked")
	public long queryBusinessCount(Map map) throws Exception {
		Map r = (Map)getSqlMapClient().queryForObject("business_sqlmap.query_BusinessCount", map);
		return Long.parseLong(r.get("COUNT").toString());
	}
	
	@SuppressWarnings("unchecked")
	public List queryBusinessPage(Map map) throws Exception {
		List list = getSqlMapClient().queryForList("business_sqlmap.query_BusinessPage", map);
		return addition(list);
	}
	
	public long addBusiness(Map map) throws Exception {
		Object obj = getSqlMapClient().insert("business_sqlmap.insert_Business", map);
		return obj==null ? 0 : Long.parseLong(obj.toString());
	}
	
	@SuppressWarnings("unchecked")
	public long delBusiness(Map map) throws Exception {
		getSqlMapClient().update("business_sqlmap.delete_business_display", map);
		return 1;
	}
	
	@SuppressWarnings("unchecked")
	public long updateBusiness(Map map) throws Exception
	{
		Object obj = getSqlMapClient().insert("business_sqlmap.update_Business", map);
		return obj==null ? 0 : Long.parseLong(obj.toString());
	}
	/**
	 * �ڲ�ʹ��
	 * @param list
	 * @return
	 */
	@SuppressWarnings("unchecked")	
	private List addition(List list) {		
		if(list == null) return null;
		
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH");
		for(int n = 0; n < list.size(); ++n) {
			Map map = (Map)list.get(n);
			map.put("INPUT_TIME_STR", format.format(map.get("INPUT_TIME")));
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	public String lockBusiness(Map map) throws Exception {
		
		String business_id = SZUtility.toString(map.get("BUSINESS_ID"));
		String lockflag = SZUtility.toString(map.get("LOCKFLAG"));
		String operate_manager = SZUtility.toString(map.get("OPERATE_MANAGER"));
		String force_unlock = SZUtility.toString(map.get("FORCE_UNLOCK"));
		
		if(business_id.equals("") || operate_manager.equals("")) {
			return "参数错误";
		}
		
		String sql = String.format("select lock_manager from business where business_id = %s", business_id);
		Map r = (Map)commonDAO.queryForObject(sql);		
		if(r == null) return "查询不到商家";		
		
		if(lockflag.equals("1")) {  //锁定
			if(r.get("LOCK_MANAGER") != null) {
				if(r.get("LOCK_MANAGER").toString().equals(operate_manager)) { //已经被自己锁定
					return null;
				} else {
					return "该商家已经被他人锁定";
				}
			}
			
			Map param = new HashMap();
			param.put("LOCK_MANAGER", operate_manager);
			param.put("BUSINESS_ID", business_id);
			getSqlMapClient().update("business_sqlmap.update_LockManager", param);
			
			return null;			
		} else if (lockflag.equals("0")){ //解锁
			
			if(r.get("LOCK_MANAGER") == null) {
				return "该商家未被锁定";
			} else {
				if(!r.get("LOCK_MANAGER").toString().equals(operate_manager) && !force_unlock.equals("1")) {
					//不是自己锁定的且没有强制解锁标志־
					return "该商家目前被他人锁定，不能进行解锁";				
				}
			}
			
			getSqlMapClient().update("business_sqlmap.update_LockManagerToNULL", business_id);
			return null;
		} else {
			return "未知的操作类型";
		}
		
	}
	
	@SuppressWarnings("unchecked")
	public List queryBusinessInfo(Map map) throws Exception {
		return getSqlMapClient().queryForList("business_sqlmap.query_BusinessInfo", map);
	}
}
